package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.NewSysFile;
import com.ruoyi.system.service.ISysFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;


@RestController
@RequestMapping("/system/file")
public class SysFileController extends BaseController {

    @Value("${ruoyi.profile}")
    private String profile;

    private final static Logger log = LoggerFactory.getLogger(SysFileController.class);

    /**
     * 删除上传的文件
     *
     * @param filePath 文件路径，如 "upload/2025/10/09/xxx.png"
     * @return 结果
     */
    @DeleteMapping("/delete")
    public AjaxResult deleteFile(@RequestParam("filePath") String filePath) {
        try {
            // 安全检查：防止路径穿越攻击（如 ../../../etc/passwd）
            if (filePath.contains("../") || filePath.contains("..\\")) {
                return AjaxResult.error("非法文件路径！");
            }

            Path file = Paths.get(profile, filePath);

            if (Files.exists(file)) {
                Files.delete(file);
//                deleteFile(file.toString());
                log.info("删除文件成功: {}", file);
                return AjaxResult.success("文件删除成功");
            } else {
                return AjaxResult.error("文件不存在");
            }
        } catch (Exception e) {
            log.error("删除文件失败", e);
            return AjaxResult.error("文件删除失败：" + e.getMessage());
        }
    }


    /*@Autowired
    private ISysFileService fileService;

    *//**
     * 查询文件列表
     *//*
    //@PreAuthorize("@ss.hasPermi('system:file:list')")
    @GetMapping("/list")
    public TableDataInfo list(NewSysFile file) {
        startPage();
        List<NewSysFile> list = fileService.getFileList(file);
        return getDataTable(list);
    }

    *//**
     * 导出文件列表
     *//*
    //@PreAuthorize("@ss.hasPermi('system:file:export')")
    @Log(title = "文件管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(NewSysFile file) {
        List<NewSysFile> list = fileService.getFileList(file);
        ExcelUtil<NewSysFile> util = new ExcelUtil<>(NewSysFile.class);
        return util.exportExcel(list, "文件数据");
    }

    *//**
     * 删除文件
     *//*
    //@PreAuthorize("@ss.hasPermi('system:file:remove')")
    @Log(title = "文件管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fileIds}")
    public AjaxResult remove(@PathVariable Long[] fileIds) {
        for (Long fileId : fileIds) {
            fileService.deleteFile(fileId);
        }
        return AjaxResult.success();
    }

    *//**
     * 根据业务删除文件（如删除公告附件）
     *//*
//    @PreAuthorize("@ss.hasPermi('system:file:remove')")
    @DeleteMapping("/biz")
    public AjaxResult removeByBiz(@RequestParam String bizType, @RequestParam Long bizId) {
        fileService.deleteFilesByBiz(bizType, bizId);
        return AjaxResult.success();
    }*/

}